package com.jjc.android.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.widget.ImageView
import android.widget.TextView
import com.danikula.videocache.HttpProxyCacheServer
import com.jjc.android.Constants
import com.jjc.android.R
import com.jjc.android.base.BaseActivity
import com.jjc.android.entity.VideoDto
import com.jjc.android.util.ProxyVideoCacheManager
import com.jjc.android.util.showImage
import xyz.doikki.videocontroller.StandardVideoController
import xyz.doikki.videocontroller.component.CompleteView
import xyz.doikki.videocontroller.component.ErrorView
import xyz.doikki.videocontroller.component.GestureView
import xyz.doikki.videocontroller.component.PrepareView
import xyz.doikki.videocontroller.component.VodControlView
import xyz.doikki.videoplayer.player.VideoView

class VideoPlayerActivity : BaseActivity() {

    companion object{
        fun start(context: Context,video:VideoDto){
            val intent = Intent(context,VideoPlayerActivity::class.java)
            intent.putExtra(Constants.BUNDLE_INFO,video)
            context.startActivity(intent)
        }
    }

    override val layoutId: Int = R.layout.activity_video_player
    private val createTimeTxt: TextView by lazy { findViewById<TextView>(R.id.createTimeTxt) }
    private val nameTxt: TextView by lazy { findViewById<TextView>(R.id.nameTxt) }
    private val mVideoView: VideoView by lazy { findViewById<VideoView>(R.id.videoPlayer) }
    private var videoDto:VideoDto? = null
    override fun initView(savedInstanceState: Bundle?) {
        setActivityTitle(getString(R.string.operate_video_txt))
        initVideoView()
    }
    private fun initVideoView(){
        val controller = StandardVideoController(this)
        val prepareView = PrepareView(this)
        prepareView.setClickStart()
        val thumbImage = prepareView.findViewById<ImageView>(R.id.thumb) //封面图
        controller.addControlComponent(prepareView)
        controller.addControlComponent(CompleteView(this)) //自动完成播放界面
        controller.addControlComponent(ErrorView(this)) //错误界面
        controller.addControlComponent(VodControlView(this))
        controller.addControlComponent(GestureView(this))
        mVideoView.setVideoController(controller)
        val cacheServer: HttpProxyCacheServer = ProxyVideoCacheManager.getProxy(this)
        videoDto?.let { video->
            val proxyUrl = cacheServer.getProxyUrl(video.video)
            mVideoView.setUrl(proxyUrl)
            thumbImage.showImage(video.video)
            nameTxt.text = video.name
            createTimeTxt.text = String.format("%s %s",video.time,getString(R.string.publish_txt))
        }

    }

    override fun onResume() {
        super.onResume()
        mVideoView.resume()
    }


    override fun onPause() {
        super.onPause()
        mVideoView.pause()
    }

    override fun onDestroy() {
        super.onDestroy()
        mVideoView.release()
    }

    override fun onBackPressed() {
        if (!mVideoView.onBackPressed()) {
            super.onBackPressed()
        }
    }

    override fun setupArguments(args: Bundle?) {
        videoDto = args?.getParcelable(Constants.BUNDLE_INFO)
    }
}